package com.ideal.manage.esms.service.project;

import com.ideal.manage.esms.bean.project.ParamSettingDTO;
import com.ideal.manage.esms.bean.project.UserManageDTO;
import com.ideal.manage.esms.bean.project.UserManageRecordDTO;
import com.ideal.manage.esms.bean.project.VehicleResouceDTO;
import com.ideal.manage.esms.bean.project.bo.UserInfoRecordBO;
import com.ideal.manage.esms.repository.framework.MySpecification;
import com.ideal.manage.esms.repository.framework.SpecificationOperator;
import com.ideal.manage.esms.repository.programme.*;
import com.ideal.manage.esms.util.HttpRequests;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.*;

/**
 *
 */
@Service
public class UserManageService {

    @Autowired
    private ParamSettingManageRepository paramSettingManageRepository;
    @Autowired
    private SysBrandMakerRepository sysBrandMakerRepository;
    @Autowired
    UserManageRepository userManageRepository;
    @Autowired
    UserManageRecordRepository userManageRecordRepository;
    @Autowired
    VehicleResouceRepository vehicleResouceRepository;

    public List<ParamSettingDTO> findAllParamSetting() {
        List<ParamSettingDTO> list = paramSettingManageRepository.findAll();
        return list;
    }

    /**
     * 明细
     * @param id
     * @return
     */
    public Map<String, Object> findUserDetailList(String id){
        UserManageDTO byId = userManageRepository.findById(id);

        List<UserInfoRecordBO> detailList = new LinkedList<>();
        /////////////////////////////////////////用户记录
        List<SpecificationOperator> operators = new LinkedList<>();
        Pageable pageable = new PageRequest(0, 150000);
        SpecificationOperator operator = new SpecificationOperator("customerphone", byId.getCustomerphone(), "EQ");
        operators.add(operator);
        MySpecification<UserManageRecordDTO> mySpecifications = new MySpecification<>(operators);
        Page<UserManageRecordDTO> all = userManageRecordRepository.findAll(mySpecifications,pageable);
        for(UserManageRecordDTO userManageRecordDTO: all.getContent()){
            if(userManageRecordDTO.getContent()!=null&&!"".equalsIgnoreCase(userManageRecordDTO.getContent())) {
                UserInfoRecordBO userInfoRecordBO = new UserInfoRecordBO();
                userInfoRecordBO.setContent(userManageRecordDTO.getContent());
                userInfoRecordBO.setName(userManageRecordDTO.getUpdateUser());
                try {
                    userInfoRecordBO.setTime(org.apache.commons.lang3.time.DateUtils.parseDate(userManageRecordDTO.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                detailList.add(userInfoRecordBO);
            }
            if(userManageRecordDTO.getSuggest()!=null&&!"".equalsIgnoreCase(userManageRecordDTO.getSuggest())) {
                UserInfoRecordBO userInfoRecordBO = new UserInfoRecordBO();
                userInfoRecordBO.setContent(userManageRecordDTO.getSuggest());
                userInfoRecordBO.setName(userManageRecordDTO.getAudit_user());
                try {
                    userInfoRecordBO.setTime(org.apache.commons.lang3.time.DateUtils.parseDate(userManageRecordDTO.getAudit_time(), "yyyy-MM-dd HH:mm:ss"));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                detailList.add(userInfoRecordBO);
            }
        }
        ///////////////////////车型到店记录
        //用户需求车型
        String carmodel = byId.getCarmodel();
        carmodel = carmodel.replaceAll(" ", "-");
        List<SpecificationOperator> operators2 = new LinkedList<>();
        SpecificationOperator operator2 = new SpecificationOperator("brand", carmodel, "EQ");
        operators2.add(operator2);
        MySpecification<VehicleResouceDTO> mySpecifications2 = new MySpecification<>(operators2);
        Page<VehicleResouceDTO> all1 = vehicleResouceRepository.findAll(mySpecifications2, pageable);
        for(VehicleResouceDTO resouceDTO: all1.getContent()){
            UserInfoRecordBO userInfoRecordBO = new UserInfoRecordBO();
            userInfoRecordBO.setContent(resouceDTO.getVehicledesc());
            userInfoRecordBO.setHref(resouceDTO.getUrldress());
            userInfoRecordBO.setName(resouceDTO.getUserid());
            userInfoRecordBO.setTime(resouceDTO.getCreatetime());
            detailList.add(userInfoRecordBO);
        }
        Collections.sort(detailList);
        Map<String,Object> detailMap = new HashMap<>(2);
        detailMap.put("userInfo", byId);
        detailMap.put("infoList", detailList);
        return detailMap;
    }
    public void deleteById(String id) {
        userManageRepository.delete(id);
    }


    public List<String> findStateList(){
        String hql = "select distinct state_name statename from t_user_manager";
        List<UserManageDTO> stateListDto = userManageRepository.selectAllBySql(hql, null, UserManageDTO.class);
        List<String> stateList = new ArrayList<String>();
        for(UserManageDTO state: stateListDto){
            System.out.println("statename:"+state.getStatename());
            stateList.add(state.getStatename());
        }
        return stateList;
    }

    public Page<UserManageDTO> findAllParameterInfos(int pageNum, HttpServletRequest request) {
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        Sort sort = new Sort(Sort.Direction.DESC, "newtime");
        SpecificationOperator operator = new SpecificationOperator("isValid", "1", "EQ");
//        operators.add(operator);
        for(SpecificationOperator op: operators){
            System.out.println("key:"+op.getKey());
            if(op.getKey().equals("newtime")){
                Date dd = DateUtil.parseYYYYMMDDDate(op.getValue().toString());
                op.setValue(dd);
            }
        }

        MySpecification<UserManageDTO> mySpecifications = new MySpecification<>(operators);
        Pageable pageable = new PageRequest(pageNum, 15, sort);
        Page<UserManageDTO> page = userManageRepository.findAll(mySpecifications, pageable);
        return page;
    }
}
